package com.zzh.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;

import androidx.annotation.Nullable;

import com.zzh.tools.system.ToolScreen;


/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2021-03-11
 * @desc 线段  支持横向和纵向
 */
public class LineSegmentView extends View {

    public LineSegmentView(Context context) {
        this(context, null);
    }

    public LineSegmentView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public LineSegmentView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    // 色值 1
    private int lsColorS;
    // 色值 2
    private int lsColorP;
    // 是纵向还是横向  vertical -》 纵向 ，horizontal -》 横向
    private int lsOrientation;
    // 长度 1
    private float lsLengthS;
    // 长度 2
    private float lsLengthP;

    private Paint mPaintS;
    private Paint mPaintP;

    private void init(Context context, AttributeSet attrs) {
        int dip2px10 = ToolScreen.dip2px(context, 10);
        int dip2px5 = ToolScreen.dip2px(context, 5);
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LineSegmentView);
        lsColorS = a.getColor(R.styleable.LineSegmentView_lsColorS, Color.parseColor("#e5e5e5"));
        lsColorP = a.getColor(R.styleable.LineSegmentView_lsColorP, Color.TRANSPARENT);
        lsOrientation = a.getInt(R.styleable.LineSegmentView_lsOrientation, 1);
        lsLengthS = a.getDimension(R.styleable.LineSegmentView_lsLengthS, dip2px10);
        lsLengthS = lsLengthS <= 0 ? dip2px10 : lsLengthS;
        lsLengthP = a.getDimension(R.styleable.LineSegmentView_lsColorP, dip2px5);
        lsLengthP = lsLengthP <= 0 ? dip2px5 : lsLengthP;
        a.recycle();

        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                getViewTreeObserver().removeOnGlobalLayoutListener(this);
                if (lsOrientation == 1) {
                    if (getMeasuredHeight() <= 0) {
                        getLayoutParams().height = 1;
                        requestLayout();
                    }
                }
                if (lsOrientation == 2) {
                    if (getMeasuredWidth() <= 0) {
                        getLayoutParams().width = 1;
                        requestLayout();
                    }
                }
            }
        });
        initPaint();
    }

    private void initPaint() {
        mPaintS = new Paint();
        mPaintS.setColor(lsColorS);
        mPaintS.setAntiAlias(true);

        mPaintP = new Paint();
        mPaintP.setColor(lsColorP);
        mPaintP.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        if (width <= 0 || height <= 0) {
            return;
        }
        int nowLength = 0;
        switch (lsOrientation) {
            case 1:
                // 横向
                while (nowLength <= width) {
                    canvas.drawRect(nowLength, 0, nowLength += lsLengthS, height, mPaintS);
                    canvas.drawRect(nowLength, 0, nowLength += lsLengthP, height, mPaintP);
                }
                break;
            case 2:
                // 纵向
                while (nowLength <= height) {
                    canvas.drawRect(0, nowLength, width, nowLength += lsLengthS, mPaintS);
                    canvas.drawRect(0, nowLength, width, nowLength += lsLengthP, mPaintP);
                }
                break;
            default:
                break;
        }
    }
}
